package tech.luoyu.ki.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import tech.luoyu.ki.entity.auto.SysUser;
import tech.luoyu.ki.service.UserService;
import tech.luoyu.ki.util.CacheUtil;
import tech.luoyu.ki.vo.ReturnResult;

@Controller
public class UserController extends BaseController {

	private static final Log LOG = LogFactory.getLog(UserController.class);

	@Resource
	private UserService userService;

	@RequestMapping(value = "user/login", method = RequestMethod.GET)
	public ModelAndView login(HttpServletRequest request) {
		return new ModelAndView("user/login");
	}

	@RequestMapping(value = "user/login", method = RequestMethod.POST)
	public ModelAndView login(HttpServletRequest request, String username, String password, boolean rememberMe) {
		Exception exception = null;
		try {
			SecurityUtils.getSubject().login(new UsernamePasswordToken(username, password, rememberMe)); // 完成登录
		} catch (Exception e) {
			exception = e;
		}

		String msg = "";
		if (exception != null) {
			if (exception instanceof UnknownAccountException) {
				System.out.println("UnknownAccountException -- > 账号不存在");
				msg = "账号不存在";
			} else if (exception instanceof IncorrectCredentialsException) {
				System.out.println("IncorrectCredentialsException -- > 密码不正确");
				msg = "密码不正确";
			} else {
				msg = "else >> " + exception;
				System.out.println("else -- >" + exception.getMessage());
				exception.printStackTrace();
			}
		}

		Map<String, Object> model = new HashMap<String, Object>();
		model.put("error", msg);
		return new ModelAndView("user/login", model);
	}

	@RequestMapping(value = "user/add", method = { RequestMethod.GET, RequestMethod.POST })
	public String add() {
		return "user/add";
	}

	@RequestMapping(value = "api/user/add", method = { RequestMethod.PUT, RequestMethod.POST })
	@ResponseBody
	@CacheEvict(value = "user", allEntries = true)
	public ReturnResult add(@Valid SysUser user) {
		return userService.add(user);
	}

	@RequestMapping(value = "user/cacheList", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	@Cacheable(value = "user")
	public ReturnResult cacheList() {
		return userService.list();
	}

	@RequestMapping(value = "user/list", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView list() {
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("result", userService.list());
		return new ModelAndView("user/list", model);
	}

	@RequestMapping(value = "user/testShiro", method = { RequestMethod.GET, RequestMethod.POST })
	public String testShiro() {
		return "user/testShiro";
	}

	@RequiresRoles("noExistRole")
	@ResponseBody
	@RequestMapping(value = "user/testRequiresRolesNotExist", method = { RequestMethod.GET, RequestMethod.POST })
	public ReturnResult testRequiresRolesNotExist() {
		ReturnResult rs = new ReturnResult();
		rs.setMessage("noExistRole");
		return rs;
	}

	@RequiresRoles("testAdmin")
	@ResponseBody
	@RequestMapping(value = "user/testRequiresRolesExist", method = { RequestMethod.GET, RequestMethod.POST })
	public ReturnResult testRequiresRolesExist() {
		ReturnResult rs = new ReturnResult();
		rs.setMessage("ExistRole");
		return rs;
	}

	@RequiresPermissions("notExistPermission")
	@ResponseBody
	@RequestMapping(value = "user/testRequiresPermissionsNotExist", method = { RequestMethod.GET, RequestMethod.POST })
	public ReturnResult testRequiresPermissionsNotExist() {
		ReturnResult rs = new ReturnResult();
		rs.setMessage("notExistPermission");
		return rs;
	}

	@RequiresPermissions("test:Permission")
	@ResponseBody
	@RequestMapping(value = "user/testRequiresPermissionsExist", method = { RequestMethod.GET, RequestMethod.POST })
	public ReturnResult testRequiresPermissionsExist() {
		ReturnResult rs = new ReturnResult();
		rs.setMessage("ExistPermission");
		return rs;
	}
}
